package com.youlemall.sellergoods.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.mapper.*;
import com.youlemall.pojo.*;
import com.youlemall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 商品实现服务
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/19 21:12
 * 4
 */

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Autowired
	private GoodsDescMapper goodsDescMapper;

	@Autowired
	private ItemCatMapper itemCatMapper;

	@Autowired
	private ItemMapper itemMapper;

	@Autowired
	private BrandMapper brandMapper;

	@Autowired
	private SellerMapper sellerMapper;



	@Override
	public void save(Goods goods) {
		//设置状态码
		goods.setAuditStatus("0");
		//保存商品
		goodsMapper.insertSelective(goods);

		//保存商品描述
		GoodsDesc goodsDesc = goods.getGoodsDesc();
		goodsDesc.setGoodsId(goods.getId());
		goodsDescMapper.insertSelective(goodsDesc);

		if("1".equals(goods.getIsEnableSpec())) {
			List<Item> items = goods.getItems();
			for (Item item : items) {
				StringBuilder sb = new StringBuilder();
				sb.append(goods.getGoodsName());
				Map<String, Object> map = JSON.parseObject(item.getSpec(), Map.class);
				for (Object value : map.values()) {
					sb.append("" + value);
				}
				item.setTitle(sb.toString());
				setItemInfo(item, goods);
				itemMapper.insertSelective(item);
			}
		}else{
			Item item = new Item();
			item.setTitle(goods.getGoodsName());
			item.setPrice(goods.getPrice());
			item.setNum(9999);
			item.setStatus("1");
			item.setIsDefault("1");
			item.setSpec("{}");
			setItemInfo(item,goods);
			itemMapper.insertSelective(item);
		}
	}


	public void setItemInfo(Item item, Goods goods){
		String itemImages = goods.getGoodsDesc().getItemImages();
		List<Map> imagesList = JSON.parseArray(itemImages, Map.class);
		if(imagesList!=null && imagesList.size()>0){
			item.setImage(imagesList.get(0).get("url").toString());
		}

		item.setCategoryid(goods.getCategory3Id());
		item.setCreateTime(new Date());
		item.setUpdateTime(item.getCreateTime());
		item.setGoodsId(goods.getId());
		item.setSellerId(goods.getSellerId());

		ItemCat itemCat = itemCatMapper.selectByPrimaryKey(goods.getCategory3Id());
		item.setCategory(itemCat!=null? itemCat.getName():"");

		Brand brand = brandMapper.selectByPrimaryKey(goods.getBrandId());
		item.setBrand(brand!=null? brand.getName():"");

		Seller seller = sellerMapper.selectByPrimaryKey(goods.getSellerId());
		item.setSeller(seller!=null?seller.getNickName():"");


	}

	@Override
	public void update(Goods goods) {

	}

	@Override
	public void delete(Serializable id) {

	}

	@Override
	public void deleteAll(Serializable[] ids) {

	}

	@Override
	public Goods findOne(Serializable id) {
		return null;
	}

	@Override
	public List<Goods> findAll() {
		return null;
	}

	@Override
	public PageResult findByPage(Goods goods, int page, int rows) {
		try {
			PageInfo<Map<String,Object>> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect() {
				@Override
				public void doSelect() {
					goodsMapper.findAll(goods);
				}
			});
			List<Map<String,Object>> maps =pageInfo.getList();
			for (Map<String, Object> map : maps) {
				ItemCat itemCat1 = itemCatMapper.selectByPrimaryKey(map.get("category1Id"));
				map.put("category1Name",itemCat1!=null ? itemCat1.getName():"");

				ItemCat itemCat2 = itemCatMapper.selectByPrimaryKey(map.get("category2Id"));
				map.put("category2Name",itemCat2!=null ? itemCat2.getName():"");

				ItemCat itemCat3 = itemCatMapper.selectByPrimaryKey(map.get("category3Id"));
				map.put("category3Name",itemCat3!=null ? itemCat3.getName():"");
			}
			return new PageResult(pageInfo.getPages(),pageInfo.getList());
		} catch (Exception e) {
			throw  new RuntimeException(e);
		}
	}

	@Override
	public void updateStatus(String columnName,Long[] ids, String status) {
		goodsMapper.updateStatus(columnName,ids,status);
	}
}
